home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / filbx2 / filebox.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-08  |  11.1 KB  |  363 lines

  1. VERSION 2.00
  2. Begin Form FileBox 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Open File"
  5.    ClientHeight    =   2775
  6.    ClientLeft      =   3420
  7.    ClientTop       =   945
  8.    ClientWidth     =   5565
  9.    Height          =   3180
  10.    Icon            =   FILEBOX.FRX:0000
  11.    Left            =   3360
  12.    LinkMode        =   1  'Source
  13.    LinkTopic       =   "Form1"
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   2775
  17.    ScaleWidth      =   5565
  18.    Top             =   600
  19.    Width           =   5685
  20.    Begin DirListBox Dir1 
  21.       Height          =   280
  22.       Left            =   3930
  23.       TabIndex        =   9
  24.       Top             =   1320
  25.       Visible         =   0   'False
  26.       Width           =   1470
  27.    End
  28.    Begin ListBox List1 
  29.       Height          =   1395
  30.       Left            =   2040
  31.       TabIndex        =   5
  32.       Top             =   1180
  33.       Width           =   1815
  34.    End
  35.    Begin DriveListBox Drive1 
  36.       Height          =   360
  37.       Left            =   3915
  38.       TabIndex        =   8
  39.       Top             =   980
  40.       Visible         =   0   'False
  41.       Width           =   1500
  42.    End
  43.    Begin FileListBox File1 
  44.       Height          =   1785
  45.       Left            =   210
  46.       TabIndex        =   3
  47.       Top             =   820
  48.       Width           =   1695
  49.    End
  50.    Begin CommandButton Cancel 
  51.       Cancel          =   -1  'True
  52.       Caption         =   "Cancel"
  53.       Height          =   360
  54.       Left            =   4335
  55.       TabIndex        =   7
  56.       Top             =   560
  57.       Width           =   1095
  58.    End
  59.    Begin CommandButton OK 
  60.       Caption         =   "OK"
  61.       Default         =   -1  'True
  62.       Height          =   360
  63.       Left            =   4335
  64.       TabIndex        =   6
  65.       Top             =   120
  66.       Width           =   1095
  67.    End
  68.    Begin TextBox Text1 
  69.       Height          =   300
  70.       Left            =   1320
  71.       TabIndex        =   1
  72.       Text            =   "*.*"
  73.       Top             =   140
  74.       Width           =   2760
  75.    End
  76.    Begin Label Label4 
  77.       Caption         =   "&Directories:"
  78.       Height          =   260
  79.       Left            =   2070
  80.       TabIndex        =   4
  81.       Top             =   900
  82.       Width           =   1530
  83.    End
  84.    Begin Label Label1 
  85.       Height          =   260
  86.       Left            =   1950
  87.       TabIndex        =   10
  88.       Top             =   500
  89.       Width           =   2160
  90.    End
  91.    Begin Label Label3 
  92.       Caption         =   "&Files:"
  93.       Height          =   240
  94.       Left            =   255
  95.       TabIndex        =   2
  96.       Top             =   480
  97.       Width           =   825
  98.    End
  99.    Begin Label Label2 
  100.       Caption         =   "File &Name:"
  101.       Height          =   260
  102.       Left            =   285
  103.       TabIndex        =   0
  104.       Top             =   140
  105.       Width           =   975
  106.    End
  107. '                               Filebox/Filebox2 by
  108. '                                   Thomas Kiehl
  109. '                                   P.O. Box 693
  110. '                         Indian Rocks Beach, FL  34635
  111. '                                  CIS: 73215,427
  112. 'This File Open Dialog Box Form and associated modules and forms are hereby released
  113. 'to the public domain to be used as seen fit by those who may use it, provided that
  114. 'such user understands that the author expresses no warranty, promise or claim of
  115. 'liability for its use, consequental use and/or damages to hardware, software or data.
  116. DefInt A-Z
  117. ' FILEBOX declarations and constants
  118. Dim LastChanged
  119. Dim LastPattern As String
  120. Dim CurrDir As String
  121. Const ASCII_ENTER = 13
  122. Const WM_USER = &H400
  123. Const LB_RESETCONTENT = WM_USER + 5
  124. Const TEXT_CHANGED = 0
  125. Const FILE_CHANGED = 1
  126. Const DIR_CHANGED = 2
  127. Declare Function SendMessage% Lib "user" (ByVal hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam&)
  128. Declare Function GetFocus% Lib "user" ()
  129. Declare Function PutFocus% Lib "user" Alias "SetFocus" (ByVal hWnd%)
  130. Sub Cancel_Click ()
  131.     Unload Filebox
  132. End Sub
  133. Sub ClearListBox (Ctrl As Control)
  134.   If Ctrl.Visible Then
  135.     hWndOld = GetFocus()
  136.     list1.SetFocus
  137.     x = SendMessage(GetFocus(), LB_RESETCONTENT, 0, 0)
  138.     x = PutFocus(hWndOld)
  139.   End If
  140. End Sub
  141. Sub Command1_Click ()
  142.     Unload Filebox
  143. End Sub
  144. Sub Dir1_change ()
  145.   ChDir (Dir1.path)
  146.   file1.path = Dir1.path
  147.   Label1.Caption = file1.path
  148.   List1_Update
  149. End Sub
  150. Sub Drive1_Change ()
  151.  On Error Resume Next
  152.  Dir1.path = CurDir$(drive1.drive)
  153.  If Err Then                'chances of an error getting here are slim
  154.   MsgBox Error$
  155.   drive1.drive = Dir1.path
  156.  End If
  157.  List1_Update
  158. End Sub
  159. Sub File1_Click ()
  160.     LastChanged = FILE_CHANGED
  161.     If file1.Listindex >= 0 Then    'zero based filename index
  162.         text1.text = file1.filename
  163.     End If
  164.     If text1.text = "" Then
  165.         OK.enabled = False
  166.     Else
  167.         OK.enabled = True
  168.     End If
  169. End Sub
  170. Sub File1_DblClick ()
  171.     LastChanged = FILE_CHANGED
  172.     OK_Click
  173. End Sub
  174. Sub File1_KeyPress (KeyAscii As Integer)
  175.     LastChanged = FILE_CHANGED
  176.     If text1.text = "" Then
  177.         OK.enabled = False
  178.     Else
  179.         OK.enabled = True
  180.     End If
  181. End Sub
  182. Sub Form_Load ()
  183.   Filebox.top = 1240
  184.   Filebox.left = 2592
  185.   Filebox.height = 3240
  186.   Filebox.width = 5640
  187.   LastPattern = "*.*"
  188.   file1.Pattern = LastPattern
  189.   List1_Update
  190.   Label1.Caption = file1.path
  191.   text1.selstart = 0
  192.   text1.sellength = Len(text1.text)
  193.   OK.enabled = True
  194.   LastChanged = TEXT_CHANGED
  195. End Sub
  196. Sub List1_Click ()
  197.   Dim startpos As Integer
  198.   LastChanged = DIR_CHANGED
  199.   OK.enabled = True
  200.   If list1.text = "[..]" Then            ' Change to the parent directory
  201.     text1.text = "..\" + file1.Pattern
  202.   Else
  203.     If Left$(list1.text, 2) = "[-" Then   ' This is a drive spec
  204.         text1.text = Mid$(list1.text, 3, 1) + ":" + file1.Pattern
  205.     Else ' This is a subdirectory of the current directory
  206.         startpos = Len(CurrDir) + 2
  207.         If list1.List(0) = "[..]" Then
  208.             text1.text = Mid$(Dir1.List((list1.Listindex) - 1), startpos) + "\" + file1.Pattern
  209.         Else
  210.             text1.text = Mid$(Dir1.List(list1.Listindex), startpos - 1) + "\" + file1.Pattern
  211.         End If
  212.     End If
  213.   End If
  214. End Sub
  215. Sub List1_Dblclick ()
  216.   LastChanged = DIR_CHANGED
  217.   If list1.text = "[..]" Then                     'the parent directory
  218.     Dir1.path = Dir1.List(-2)
  219.     Dir1_change
  220.   Else
  221.     If Left$(list1.text, 2) = "[-" Then           'this is a drive spec
  222.       On Error GoTo list1_error
  223.       Dummy$ = Dir$(Mid$(list1.text, 3, 1) + ":") 'error if door is open
  224.                                                   'error has been trapped out
  225.       drive1.drive = Mid$(list1.text, 3, 1) + ":" 'error if door is open (we did check it)
  226.     Else                                          'sub directory
  227.       If list1.List(0) = "[..]" Then              'we are not at root dir
  228.         Dir1.path = Dir1.List((list1.Listindex) - 1)
  229.       Else                                         'oh yes we are
  230.         Dir1.path = Dir1.List(list1.Listindex)
  231.       End If
  232.       Dir1_change                                  'do the event
  233.     End If
  234.   End If
  235. Exit Sub
  236. list1_error:                                        'uh oh!
  237.     Beep
  238.     If Err = FILE_NOT_FOUND Then
  239.         Button = MB_OK + MB_ICONEXCLAMATION
  240.     Else
  241.         Button = MB_ICONQUESTION + MB_RETRYCANCEL
  242.     End If
  243.     Button = MsgBox("ERROR: " + Error$(Err) + Str$(Err), Button, "FILE OPEN")
  244.     If Button = IDRETRY Then
  245.       Resume
  246.     End If
  247.     On Error GoTo 0
  248.     Exit Sub
  249. End Sub
  250. Sub List1_Update ()
  251.   ClearListBox list1
  252.   CurrDir = Dir1.path
  253.   If Len(CurrDir) > 3 Then
  254.     list1.AddItem "[..]"
  255.     DirPos = Len(CurrDir) + 2
  256.   Else
  257.     DirPos = 4
  258.   End If
  259.   For Count = 0 To Dir1.listcount - 1
  260.     list1.AddItem "[" + Mid$(Dir1.List(Count), DirPos) + "]"
  261.   Next Count
  262.   For Count = 0 To drive1.listcount - 1
  263.     list1.AddItem "[-" + Left$(drive1.List(Count), 1) + "-]"
  264.   Next Count
  265.   update_filespec
  266. End Sub
  267. Sub OK_Click ()
  268.   Dim temp As String
  269.   If text1.text = "" Then     'we shouldn't be here in the first place
  270.     Exit Sub
  271.   End If
  272.   If LastChanged <> FILE_CHANGED Then
  273.         Is_Valid = Valid_FileSpec()   'try and open filespec
  274.   Else
  275.       WorkFile = file1.path
  276.       If Right$(WorkFile, 1) <> "\" Then
  277.         WorkFile = WorkFile + "\"
  278.       End If
  279.       WorkFile = WorkFile + file1.filename
  280.       Unload Filebox
  281.   End If
  282. Exit Sub
  283. End                 'left over from development
  284. Drive_Error:
  285.     MsgBox Error$(Err)
  286.     Exit Sub
  287. dir_change_error:
  288.     Beep
  289.     If Err = FILE_NOT_FOUND Then
  290.         Button = MB_OK + MB_ICONEXCLAMATION
  291.     Else
  292.         Button = MB_ICONQUESTION + MB_RETRYCANCEL
  293.     End If
  294.     Button = MsgBox("ERROR: " + Error$(Err) + Str$(Err), Button, "FILE OPEN")
  295.     If Button = IDRETRY Then
  296.       Resume
  297.     End If
  298.     On Error GoTo 0
  299.     Exit Sub
  300. End Sub
  301. Sub Text1_Change ()
  302.     If LastChanged = TEXT_CHANGED Then
  303.         If text1.text = "" Then
  304.           OK.enabled = False
  305.         Else
  306.           OK.enabled = True
  307.         End If
  308.     End If
  309. End Sub
  310. Sub Text1_KeyDown (keycode As Integer, Shift As Integer)
  311.   LastChanged = TEXT_CHANGED
  312. End Sub
  313. Sub update_filespec ()
  314. Dim SelPath As String, CurPath As String, slash As String 'slash is null at this point
  315.   CurPath = Label1.Caption
  316.   SelPath = list1.List(list1.Listindex)
  317.   Select Case list1.Listindex
  318.     Case Is >= 0         'a subdirectory
  319.       I = Right$(CurPath, 1) <> "\"
  320.       file1.text = Right$(SelPath, Len(SelPath) - Len(CurPath) + I) + "\" + file1.Pattern
  321.         
  322.     Case -1             'the current directory
  323.       text1.text = file1.Pattern
  324.         
  325.     Case Is < -1        'the parent directory
  326.       SelPath = Right$(SelPath, Len(SelPath) - 2)
  327.       If Len(SelPath) > 1 Then slash = "\"
  328.       text1.text = SelPath + slash + file1.Pattern
  329.   End Select
  330. End Sub
  331. Function Valid_FileSpec ()
  332.   Dim temp As String
  333.   On Error GoTo ErrorInSpec
  334.   Valid_FileSpec = True
  335.   temp = Dir$(text1.text)
  336.   file1.filename = text1.text
  337.   ChDir file1.path                       'gets here if good path only
  338.   drive1.drive = Left$(file1.path, 2)
  339.   Dir1.path = file1.path
  340.   Valid_FileSpec = False
  341. Quit_Function:
  342.   On Error GoTo 0
  343. Exit Function
  344. ErrorInSpec:
  345.   If (Err <> FILE_NOT_FOUND) Then
  346.     Beep
  347.     Button = MB_ICONQUESTION + MB_RETRYCANCEL
  348.     Button = MsgBox("ERROR: " + Error$(Err) + Str$(Err), Button, "FILE OPEN")
  349.     If Button = IDRETRY Then
  350.       Resume
  351.     End If
  352.   Else
  353.     If Err = FILE_NOT_FOUND Then            'no other error
  354.                                             'but is was NOT a wildcard
  355.       temp = Right$(text1.text, 1)
  356.       file1.filename = Left$(text1.text, Len(text1.text) - 1) + "*"
  357.       text1.text = Left$(file1.Pattern, Len(file1.Pattern) - 1) + temp
  358.     End If
  359.     Valid_FileSpec = False
  360.   End If
  361.   Resume Quit_Function
  362. End Function
  363.